//
//  ExpresserDBModel.m
//  TengyunStation
//
//  Created by Johnny on 15/8/24.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ExpresserDBModel.h"
#import "SocketMethod.h"
#import "Singleton.h"

@implementation ExpresserDBModel

- (void)saveExpresserDBModel {
    ExpresserDBModel *expresser = [ExpresserDBModel findFirstByCriteria:[NSString stringWithFormat:@"WHERE telephone = '%@' AND websiteId = '%@'",self.telephone,[Singleton shareInstance].userMessage.websiteId]];
    self.saveday = [self todayTime];
    if (expresser == nil) {
        [super save];
    } else {
        self.pk = expresser.pk;
        [self saveOrUpdate];
    }
}

- (NSString *)todayTime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [dateFormatter stringFromDate:[NSDate date]];
}

- (id)initWithData:(NSData *)data Telephone:(NSString *)telephone {
    self = [super init];
    if (self) {
        self.telephone = telephone;
        NSInteger position = 0;
        self.expresserId = [SocketMethod stringSubdata:data Range:NSMakeRange(position, 64)];
        position += 64;
        self.expresserName = [SocketMethod stringSubdata:data Range:NSMakeRange(position, 64)];
        position += 64;
        self.expressCompanyId = [SocketMethod stringSubdata:data Range:NSMakeRange(position, 64)];
        position += 64;
        self.expressCompanyName = [SocketMethod stringSubdata:data Range:NSMakeRange(position, 64)];
        self.websiteId = [Singleton shareInstance].userMessage.websiteId;
    }
    return self;
}

- (id)initWithString:(NSString *)string {
    self = [super init];
    if (self) {
        NSArray *array = [string componentsSeparatedByString:@","];
        self.expressCompanyName = [SocketMethod subArray:array Index:0];
        self.expresserName = [SocketMethod subArray:array Index:1];
        self.telephone = [SocketMethod subArray:array Index:2];
        self.websiteId = [Singleton shareInstance].userMessage.websiteId;
    }
    return self;
}

- (id)initWithAuthodString:(NSString *)string {
    self = [super init];
    if (self) {
        NSArray *array = [string componentsSeparatedByString:@","];
        int i = 0;
        self.expressCompanyId = [SocketMethod subArray:array Index:i ++];
        self.expressCompanyName = [SocketMethod subArray:array Index:i ++];
        self.expressCompanyLogo = [SocketMethod subArray:array Index:i ++];
        self.expresserId = [SocketMethod subArray:array Index:i ++];
        self.expresserName = [SocketMethod subArray:array Index:i ++];
        self.telephone = [SocketMethod subArray:array Index:i ++];
        NSString *time = [SocketMethod subArray:array Index:i ++];
        self.efficientTime = [SocketMethod dateStringWithDateString:time];
        self.money = [[SocketMethod subArray:array Index:i ++] floatValue];
    }
    return self;
}

@end
